package jannex.sql.extensions;

import jannex.Jannex;
import jannex.internal.ExceptionFactory;
import jannex.sql.JxParameterMetaData;

import java.sql.ParameterMetaData;
import java.sql.SQLException;

public class ParameterMetaDataImpl implements JxParameterMetaData {

    private final ParameterMetaData parameterMetaData;
    private final Jannex jannex;

    public ParameterMetaDataImpl(ParameterMetaData parameterMetaData, Jannex jannex) {
        this.jannex = jannex;
        this.parameterMetaData = parameterMetaData;
    }

    @Override
    public int getParameterCount() {
        try {
            return parameterMetaData.getParameterCount();
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int isNullable(int param) {
        try {
            return parameterMetaData.isNullable(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public boolean isSigned(int param) {
        try {
            return parameterMetaData.isSigned(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int getPrecision(int param) {
        try {
            return parameterMetaData.getPrecision(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int getScale(int param) {
        try {
            return parameterMetaData.getScale(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int getParameterType(int param) {
        try {
            return parameterMetaData.getParameterType(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public String getParameterTypeName(int param) {
        try {
            return parameterMetaData.getParameterTypeName(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public String getParameterClassName(int param) {
        try {
            return parameterMetaData.getParameterClassName(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public int getParameterMode(int param) {
        try {
            return parameterMetaData.getParameterMode(param);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T unwrap(Class<T> iface) {
        try {
            if (iface.equals(ParameterMetaData.class)) {
                return (T) parameterMetaData;
            }
            return parameterMetaData.unwrap(iface);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) {
        try {
            return iface.equals(ParameterMetaData.class) || parameterMetaData.isWrapperFor(iface);
        } catch (SQLException e) {
            throw ExceptionFactory.make(e);
        }
    }
}
